package com.jinlejia.erp.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * 更多筛选片区第2列Adapter
 */
public class FilterArea2Adapter extends YfListAdapter<String> {


    public int cur_position = -1;
    private Map<Integer,Boolean> checkMap = new HashMap<>();
    public String area;

    public FilterArea2Adapter(ArrayList<String> data) {
        super(data);

    }
    public void setArea( String area ){
        this.area = area;
    }
    public void setData(ArrayList<String> data) {
        super.setData(data);
        if( !TextUtils.isEmpty(area))
        for (int i = 0; i < mData.size(); i++) {
                if ( area.equals(mData.get(i))) {
                    cur_position = i;
                    checkMap.put(i, true);
            }
        }
    }

    @Override
    public int getItemCount() {
        for( int i=0;i<mData.size();i++ ){
            if( cur_position == i ){
                checkMap.put(i,true);
            }else{
                checkMap.put(i,false);
            }
        }
        return super.getItemCount();
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filter2_3_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder) viewHolder).checkbox.setText(mData.get(i));

        if (checkMap.get(i)) {
            ((ViewHolder) viewHolder).checkbox.setChecked(true);
            ((ViewHolder) viewHolder).img.setVisibility(View.VISIBLE);
            ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.dark_gray_bg);
            if( null != onItemSelectedListener )
            onItemSelectedListener.onItemSelected(mData.get(i));
        } else {
            ((ViewHolder) viewHolder).checkbox.setChecked(false);
            ((ViewHolder) viewHolder).img.setVisibility(View.INVISIBLE);
            ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.gray_bg);
        }

        viewHolder.itemView.setTag(mData.get(i));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cur_position != -1 ) {
                    checkMap.put(cur_position,false);
                    notifyItemChanged(cur_position);
                    onItemRemoveListener.onItemRemove(mData.get(cur_position));
                }
                if( cur_position != i ){
                    cur_position = i;
                    checkMap.put(i,true);
                    notifyItemChanged(i);
                    onItemSelectedListener.onItemSelected(mData.get(i));
                }else{
                    if( cur_position != -1 )
                        onItemRemoveListener.onItemRemove(mData.get(cur_position));
                    checkMap.put(cur_position,false);
                    notifyItemChanged(cur_position);
                    cur_position = -1;
                }
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkbox;
        ImageView img;
        LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.iv_icon);
            checkbox = itemView.findViewById(R.id.checkbox);
            checkbox.setTag(false);
            layout = itemView.findViewById(R.id.layout);
        }
    }

    /**
     * 选中
     */

    public interface OnItemSelectedListener {
        void onItemSelected(String s);
    }

    private OnItemSelectedListener onItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

    /**
     * 移除
     */
    public interface OnItemRemoveListener {
        void onItemRemove(String s);
    }

    private OnItemRemoveListener onItemRemoveListener;

    public void setOnItemRemoveListener(OnItemRemoveListener listener) {
        this.onItemRemoveListener = listener;
    }

}
